﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReverseString
{
    public class WordNode
    {
        public string Data { get; set; }
        public WordNode Next { get; set; }
    }
    
    public class StackString2
    {
        private WordNode head = null;

        public void push(string value)
        {
            WordNode node = new WordNode();
            node.Data = value;

            if (head == null)
            {
                head = node;
                head.Next = null;
            }
            else
            {
                node.Next = head;
                head = node;
            }
        }

        public string Pop()
        {
            WordNode node = null;
            if (head != null)
            {
                node = head;
                head = head.Next;
            }
            return node == null? string.Empty : node.Data;
        }

        public bool HasData()
        {
            return head == null? false : true;
        }
    }

    public class StackString
    {
        private WordNode head = null;

        public void push(string value)
        {
            if (head == null)
            {
                head = new WordNode();
                head.Data = value;
                head.Next = null;
            }
            else
            {
                WordNode node = new WordNode();
                node.Data = value;
                node.Next = head;
                head = node;
            }
        }

        public bool HasData()
        {
            return head == null ? false : true;
        }

        public string Pop()
        {
            WordNode node = null;
            if (head != null)
            {
                node = head;
                head = head.Next;
            }
            return node == null ? string.Empty : node.Data;
        }
    }

    public static class ReverseString
    {
        public static void Reverse(string inputString)
        {
            if (string.IsNullOrEmpty(inputString))
            {
                throw new ArgumentNullException("inputString","string is null");
            }

            StackString2 stack = new StackString2();
            string value = string.Empty;
            foreach (char c in inputString)
            {
                if (c == ' ')
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        stack.push(value);
                    }
                    //value = c.ToString();
                    //stack.push(value);
                    value = string.Empty;
                }
                else
                {
                    value += c.ToString();
                }
            }

            if (!string.IsNullOrEmpty(value))
            {
                stack.push(value);
            }
            while (stack.HasData())
            {
                Console.WriteLine(stack.Pop());
            }
        }

        
    }
    class Program
    {
        static void Main(string[] args)
        {
            ReverseString.Reverse("you are a student");
            ReverseString.Reverse(string.Empty);
            Console.ReadKey();
        }      
    }
}
